package com.ruoyi.caiwu.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.caiwu.domain.CwDealPerDayVo;
import com.ruoyi.caiwu.domain.CwReceiptPlan;
import com.ruoyi.caiwu.dto.CwDealPerDayDetailReceiptDto;
import com.ruoyi.caiwu.dto.RequestReceiptManageDto;
import com.ruoyi.caiwu.dto.ResponseReceiptManageDto;
import com.ruoyi.caiwu.service.CwReceiptPlanService;
import com.ruoyi.caiwu.mapper.CwReceiptPlanMapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 86182
* @description 针对表【t_cw_receipt_plan(收款计划表)】的数据库操作Service实现
* @createDate 2023-05-05 10:24:54
*/
@Service
public class CwReceiptPlanServiceImpl extends ServiceImpl<CwReceiptPlanMapper, CwReceiptPlan>
    implements CwReceiptPlanService{

    @Override
    public List<CwDealPerDayDetailReceiptDto> searchCwDealPerDayDetailReceiptPage(CwDealPerDayVo vo) {
        return this.baseMapper.searchCwDealPerDayDetailReceiptPage(vo);
    }

    @Override
    public List<CwDealPerDayDetailReceiptDto> searchCwDealPerDayDetailPayPage(CwDealPerDayVo vo) {
        return this.baseMapper.searchCwDealPerDayDetailPayPage(vo);
    }

    @Override
    public Map<String, Object> receiptManage(Page page, RequestReceiptManageDto vo) {

        Map<String, Object> queryParams = new HashMap<String, Object>();
        //queryParams.put("businessId", vo.getBusinessId());
        queryParams.put("premiseName", vo.getPremiseName());
        queryParams.put("tenantName", vo.getTenantName());
        queryParams.put("skStatus", vo.getSkStatus());
        queryParams.put("billType", vo.getBillType());
        queryParams.put("contractNo", vo.getContractNo());
        queryParams.put("startDate", vo.getStartDate());
        queryParams.put("endDate", vo.getEndDate());
        queryParams.put("collectionStartDate", vo.getCollectionStartDate());
        queryParams.put("collectionEndDate", vo.getCollectionStartDate());
        queryParams.put("receiptType", vo.getReceiptType());
        queryParams.put("receiptOrderNo", vo.getReceiptOrderNo());

        IPage<ResponseReceiptManageDto> list = this.baseMapper.receiptManage(page, queryParams);

        List<ResponseReceiptManageDto> receiptManageList = this.baseMapper.receiptManages(queryParams);

        //应收金额
        BigDecimal amountReceivable = BigDecimal.ZERO;
        //实收金额
        BigDecimal amountCollected = BigDecimal.ZERO;

        for (ResponseReceiptManageDto responseReceiptManageDto : receiptManageList) {
            if(responseReceiptManageDto.getPaymentAmount() != null) {
                amountReceivable = amountReceivable.add(responseReceiptManageDto.getPaymentAmount());
//				System.out.println("应收金额："+amountReceivable);
            }
            if(responseReceiptManageDto.getSkStatus() != null && responseReceiptManageDto.getSkStatus() == 2) {
                if(responseReceiptManageDto.getPaymentAmount() != null) {
                    amountCollected = amountCollected.add(responseReceiptManageDto.getPaymentAmount());
//					System.out.println("实收金额："+amountCollected);
                }
            }
        }

        HashMap<String, Object> map = new HashMap<>();
        // 设置分页结果
        map.put("total",list.getTotal());
        map.put("records",list.getRecords());
        map.put("amountReceivable",amountReceivable);
        map.put("amountCollected",amountCollected);

        System.err.println(map);
        return map;
    }
}




